﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBAccessLib;
using System.IO;

namespace LineWatch_WinForm
{
    public partial class PositionManagementView : Form
    {
        private bool hasSHAPositionLine1 = false;
        private bool hasSHAPositionLine2 = false;
        private bool hasSHAPositionLine3 = false;

        private Dictionary<int, PositionStatusInfo> positionStatusInfoLine1 = new Dictionary<int, PositionStatusInfo>();
        private Dictionary<int, PositionStatusInfo> positionStatusInfoLine2 = new Dictionary<int, PositionStatusInfo>();
        private Dictionary<int, PositionStatusInfo> positionStatusInfoLine3 = new Dictionary<int, PositionStatusInfo>();

        public PositionManagementView()
        {
            InitializeComponent();
            this.timer.Enabled = false;

            this.InitFont();

            this.ClearPositionInfo(ConfigInfo.LineCode1);
            this.ClearPositionInfo(ConfigInfo.LineCode2);
            this.ClearPositionInfo(ConfigInfo.LineCode3);

#if DEBUG
            this.TopMost = false;
#else
            this.TopMost = true;
#endif
        }

        /// <summary>
        /// Form Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PositionManagementView_Load(object sender, EventArgs e)
        {
            this.InitOperationArea();
            this.timer.Enabled = true;
        }

        /// <summary>
        /// Init Operation Area
        /// </summary>
        private void InitOperationArea()
        {
            string positionManagement = "工程位管理看板";
            if (ConfigInfo.CurrentLineCodeCount <= 0)
            {
                this.tlpMain.Visible = false;
            }

            if (ConfigInfo.CurrentLineCodeCount >= 3)
            {
                string lineName3 = ConfigInfo.GetLineNameByCode(ConfigInfo.LineCode3);

                this.lblShiftLine3.Text = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys3);

                this.lblPositionManagement3.Text = lineName3 + positionManagement;

                this.tlpOperationArea1.Visible = true;
                this.tlpOperationArea2.Visible = true;
                this.tlpOperationArea3.Visible = true;

                this.tlpMain.ColumnStyles.Clear();
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
            }

            if (ConfigInfo.CurrentLineCodeCount >= 2)
            {
                string lineName2 = ConfigInfo.GetLineNameByCode(ConfigInfo.LineCode2);

                this.lblShiftLine2.Text = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys2);

                this.lblPositionManagement2.Text = lineName2 + positionManagement;

                if (ConfigInfo.CurrentLineCodeCount == 2)
                {
                    this.tlpOperationArea1.Visible = true;
                    this.tlpOperationArea2.Visible = true;
                    this.tlpOperationArea3.Visible = false;

                    this.tlpMain.ColumnStyles.Clear();
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 5F));
                }
            }

            if (ConfigInfo.CurrentLineCodeCount >= 1)
            {
                string lineName1 = ConfigInfo.GetLineNameByCode(ConfigInfo.LineCode1);

                this.lblShiftLine1.Text = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys1);

                this.lblPositionManagement1.Text = lineName1 + positionManagement;

                if (ConfigInfo.CurrentLineCodeCount == 1)
                {
                    this.tlpOperationArea1.Visible = true;
                    this.tlpOperationArea2.Visible = false;
                    this.tlpOperationArea3.Visible = false;

                    this.tlpMain.ColumnStyles.Clear();
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 30F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                    this.tlpMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 30F));
                }
            }
        }

        /// <summary>
        /// Init Font
        /// </summary>
        private void InitFont()
        {
            foreach (Control control in this.Controls)
            {
                if (control.GetType() == typeof(Label))
                {
                    Label lbl = (Label)control;
                    if (lbl.Tag != null)
                    {
                        this.UpdateFont(lbl);
                    }
                }

                InitFont(control);
            }
        }

        /// <summary>
        /// Init Font
        /// </summary>
        /// <param name="controlParent"></param>
        private void InitFont(Control controlParent)
        {
            foreach (Control control in controlParent.Controls)
            {
                if (control.GetType() == typeof(Label))
                {
                    Label lbl = (Label)control;
                    if (lbl.Tag != null)
                    {
                        this.UpdateFont(lbl);
                    }
                }

                InitFont(control);
            }
        }

        /// <summary>
        /// Update Font
        /// </summary>
        /// <param name="control"></param>
        private void UpdateFont(Control control)
        {
            if (control.Tag.ToString().StartsWith("FontSizeTitle"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.PositionFontSizeTitle, FontStyle.Bold);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeCaption"))
            {
                this.SetFont(control, "Microsoft YaHei", ConfigInfo.PositionFontSizeCaption, FontStyle.Bold);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeValue"))
            {
                this.SetFont(control, "Impact", ConfigInfo.PositionFontSizeValue, FontStyle.Regular);
            }
        }

        /// <summary>
        /// Set Font
        /// </summary>
        /// <param name="control"></param>
        /// <param name="familyName"></param>
        /// <param name="emSize"></param>
        /// <param name="style"></param>
        private void SetFont(Control control, string familyName, float emSize, FontStyle style)
        {
            control.Font = new System.Drawing.Font(familyName,
                                                   emSize,
                                                   style,
                                                   System.Drawing.GraphicsUnit.Point,
                                                   ((byte)(0))
                                                   );
        }

        /// <summary>
        /// Clear Position Info
        /// </summary>
        /// <param name="lineCode"></param>
        private void ClearPositionInfo(string lineCode)
        {
            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                this.hasSHAPositionLine1 = false;

                this.lblLine1Alarm1.Text = string.Empty;
                this.lblLine1Alarm2.Text = string.Empty;
                this.lblLine1Alarm3.Text = string.Empty;
                this.lblLine1Alarm4.Text = string.Empty;
                this.lblLine1Alarm5.Text = string.Empty;
                this.lblLine1Alarm6.Text = string.Empty;

                this.lblLine1Help1.Text = string.Empty;
                this.lblLine1Help2.Text = string.Empty;
                this.lblLine1Help3.Text = string.Empty;
                this.lblLine1Help4.Text = string.Empty;
                this.lblLine1Help5.Text = string.Empty;
                this.lblLine1Help6.Text = string.Empty;

                this.lblLine1Stop1.Text = string.Empty;
                this.lblLine1Stop2.Text = string.Empty;
                this.lblLine1Stop3.Text = string.Empty;
                this.lblLine1Stop4.Text = string.Empty;
                this.lblLine1Stop5.Text = string.Empty;
                this.lblLine1Stop6.Text = string.Empty;
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                this.hasSHAPositionLine2 = false;

                this.lblLine2Alarm1.Text = string.Empty;
                this.lblLine2Alarm2.Text = string.Empty;
                this.lblLine2Alarm3.Text = string.Empty;
                this.lblLine2Alarm4.Text = string.Empty;
                this.lblLine2Alarm5.Text = string.Empty;
                this.lblLine2Alarm6.Text = string.Empty;

                this.lblLine2Help1.Text = string.Empty;
                this.lblLine2Help2.Text = string.Empty;
                this.lblLine2Help3.Text = string.Empty;
                this.lblLine2Help4.Text = string.Empty;
                this.lblLine2Help5.Text = string.Empty;
                this.lblLine2Help6.Text = string.Empty;

                this.lblLine2Stop1.Text = string.Empty;
                this.lblLine2Stop2.Text = string.Empty;
                this.lblLine2Stop3.Text = string.Empty;
                this.lblLine2Stop4.Text = string.Empty;
                this.lblLine2Stop5.Text = string.Empty;
                this.lblLine2Stop6.Text = string.Empty;
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                this.hasSHAPositionLine3 = false;

                this.lblLine3Alarm1.Text = string.Empty;
                this.lblLine3Alarm2.Text = string.Empty;
                this.lblLine3Alarm3.Text = string.Empty;
                this.lblLine3Alarm4.Text = string.Empty;
                this.lblLine3Alarm5.Text = string.Empty;
                this.lblLine3Alarm6.Text = string.Empty;

                this.lblLine3Help1.Text = string.Empty;
                this.lblLine3Help2.Text = string.Empty;
                this.lblLine3Help3.Text = string.Empty;
                this.lblLine3Help4.Text = string.Empty;
                this.lblLine3Help5.Text = string.Empty;
                this.lblLine3Help6.Text = string.Empty;

                this.lblLine3Stop1.Text = string.Empty;
                this.lblLine3Stop2.Text = string.Empty;
                this.lblLine3Stop3.Text = string.Empty;
                this.lblLine3Stop4.Text = string.Empty;
                this.lblLine3Stop5.Text = string.Empty;
                this.lblLine3Stop6.Text = string.Empty;
            }

            //this.Refresh();
            //this.Update();
        }

        /// <summary>
        /// Get Position Data Row
        /// </summary>
        /// <param name="position"></param>
        /// <param name="lineCode"></param>
        /// <returns></returns>
        private DataRow GetPositionDataRow(int position, string lineCode)
        {
            if (ConfigInfo.DtAllPositions != null)
            {
                foreach (DataRow row in ConfigInfo.DtAllPositions.Rows)
                {
                    int dmPosition = 0;

                    if (int.TryParse(row["Content1"].ToString().Trim(), out dmPosition))
                    {
                        if ((position == dmPosition)
                            && string.Equals(lineCode.Trim(), row["Line_Code"].ToString().Trim()))
                        {
                            return row;
                        }
                    }
                }
            }

            return null;
        }

        /// <summary>
        /// Get Position Type
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private PositionType GetPositionType(DataRow row)
        {
            if (row != null)
            {
                if (string.Equals("1", row["Content2"].ToString().Trim()))
                {
                    return PositionType.Help;
                }
                else if (string.Equals("1", row["Content3"].ToString().Trim()))
                {
                    return PositionType.Alarm;
                }
                else if (string.Equals("1", row["Content4"].ToString().Trim()))
                {
                    return PositionType.Stop;
                }
                else
                {
                    return PositionType.Unknown;
                }
            }

            return PositionType.Null;
        }

        /// <summary>
        /// Get Position Desc
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private string GetPositionDesc(DataRow row, bool isShowPositionName)
        {
            if (row != null)
            {
                string fPositionID = row["FPositionID"].ToString().Trim();

                string positionName = row["Position"].ToString().Trim();

                ////if (isShowPositionName)
                ////{
                ////    return fPositionID + " " + positionName + ";";
                ////}
                ////else
                ////{
                ////    return fPositionID + ";";
                ////}

                return positionName + ";";
            }

            return string.Empty;
        }

        /// <summary>
        /// Get SPositionID
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private string GetSPositionID(DataRow row)
        {
            if (row != null)
            {
                string sPositionID = row["SPositionID"].ToString().Trim();

                return sPositionID;
            }

            return string.Empty;
        }

        /// <summary>
        /// Get FPositionID
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private string GetFPositionID(DataRow row)
        {
            if (row != null)
            {
                string fPositionID = row["FPositionID"].ToString().Trim();

                return fPositionID;
            }

            return string.Empty;
        }

        /// <summary>
        /// Update Alarm Position Info
        /// </summary>
        /// <param name="positionDesc"></param>
        /// <param name="lineCode"></param>
        private void UpdateAlarmPositionInfo(string positionDesc, string lineCode)
        {
            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                this.hasSHAPositionLine1 = true;

                if (string.IsNullOrWhiteSpace(this.lblLine1Alarm1.Text))
                {
                    this.lblLine1Alarm1.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Alarm4.Text))
                {
                    this.lblLine1Alarm4.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Alarm2.Text))
                {
                    this.lblLine1Alarm2.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Alarm5.Text))
                {
                    this.lblLine1Alarm5.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Alarm3.Text))
                {
                    this.lblLine1Alarm3.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Alarm6.Text))
                {
                    this.lblLine1Alarm6.Text = positionDesc;
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                this.hasSHAPositionLine2 = true;

                if (string.IsNullOrWhiteSpace(this.lblLine2Alarm1.Text))
                {
                    this.lblLine2Alarm1.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Alarm4.Text))
                {
                    this.lblLine2Alarm4.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Alarm2.Text))
                {
                    this.lblLine2Alarm2.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Alarm5.Text))
                {
                    this.lblLine2Alarm5.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Alarm3.Text))
                {
                    this.lblLine2Alarm3.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Alarm6.Text))
                {
                    this.lblLine2Alarm6.Text = positionDesc;
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                this.hasSHAPositionLine3 = true;

                if (string.IsNullOrWhiteSpace(this.lblLine3Alarm1.Text))
                {
                    this.lblLine3Alarm1.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Alarm4.Text))
                {
                    this.lblLine3Alarm4.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Alarm2.Text))
                {
                    this.lblLine3Alarm2.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Alarm5.Text))
                {
                    this.lblLine3Alarm5.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Alarm3.Text))
                {
                    this.lblLine3Alarm3.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Alarm6.Text))
                {
                    this.lblLine3Alarm6.Text = positionDesc;
                }
            }

            this.Refresh();
            this.Update();

            ////this.PlayMusic(lineCode, PositionType.Alarm);
        }

        /// <summary>
        /// Update Help Position Info
        /// </summary>
        /// <param name="positionDesc"></param>
        /// <param name="lineCode"></param>
        private void UpdateHelpPositionInfo(string positionDesc, string lineCode)
        {
            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                this.hasSHAPositionLine1 = true;

                if (string.IsNullOrWhiteSpace(this.lblLine1Help1.Text))
                {
                    this.lblLine1Help1.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Help4.Text))
                {
                    this.lblLine1Help4.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Help2.Text))
                {
                    this.lblLine1Help2.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Help5.Text))
                {
                    this.lblLine1Help5.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Help3.Text))
                {
                    this.lblLine1Help3.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Help6.Text))
                {
                    this.lblLine1Help6.Text = positionDesc;
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                this.hasSHAPositionLine2 = true;

                if (string.IsNullOrWhiteSpace(this.lblLine2Help1.Text))
                {
                    this.lblLine2Help1.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Help4.Text))
                {
                    this.lblLine2Help4.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Help2.Text))
                {
                    this.lblLine2Help2.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Help5.Text))
                {
                    this.lblLine2Help5.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Help3.Text))
                {
                    this.lblLine2Help3.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Help6.Text))
                {
                    this.lblLine2Help6.Text = positionDesc;
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                this.hasSHAPositionLine3 = true;

                if (string.IsNullOrWhiteSpace(this.lblLine3Help1.Text))
                {
                    this.lblLine3Help1.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Help4.Text))
                {
                    this.lblLine3Help4.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Help2.Text))
                {
                    this.lblLine3Help2.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Help5.Text))
                {
                    this.lblLine3Help5.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Help3.Text))
                {
                    this.lblLine3Help3.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Help6.Text))
                {
                    this.lblLine3Help6.Text = positionDesc;
                }
            }

            this.Refresh();
            this.Update();

            ////this.PlayMusic(lineCode, PositionType.Help);
        }

        /// <summary>
        /// Update Stop Position Info
        /// </summary>
        /// <param name="positionDesc"></param>
        /// <param name="lineCode"></param>
        private void UpdateStopPositionInfo(string positionDesc, string lineCode)
        {
            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                this.hasSHAPositionLine1 = true;

                if (string.IsNullOrWhiteSpace(this.lblLine1Stop1.Text))
                {
                    this.lblLine1Stop1.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Stop4.Text))
                {
                    this.lblLine1Stop4.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Stop2.Text))
                {
                    this.lblLine1Stop2.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Stop5.Text))
                {
                    this.lblLine1Stop5.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Stop3.Text))
                {
                    this.lblLine1Stop3.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine1Stop6.Text))
                {
                    this.lblLine1Stop6.Text = positionDesc;
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                this.hasSHAPositionLine2 = true;

                if (string.IsNullOrWhiteSpace(this.lblLine2Stop1.Text))
                {
                    this.lblLine2Stop1.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Stop4.Text))
                {
                    this.lblLine2Stop4.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Stop2.Text))
                {
                    this.lblLine2Stop2.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Stop5.Text))
                {
                    this.lblLine2Stop5.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Stop3.Text))
                {
                    this.lblLine2Stop3.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine2Stop6.Text))
                {
                    this.lblLine2Stop6.Text = positionDesc;
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                this.hasSHAPositionLine3 = true;

                if (string.IsNullOrWhiteSpace(this.lblLine3Stop1.Text))
                {
                    this.lblLine3Stop1.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Stop4.Text))
                {
                    this.lblLine3Stop4.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Stop2.Text))
                {
                    this.lblLine3Stop2.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Stop5.Text))
                {
                    this.lblLine3Stop5.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Stop3.Text))
                {
                    this.lblLine3Stop3.Text = positionDesc;
                }
                else if (string.IsNullOrWhiteSpace(this.lblLine3Stop6.Text))
                {
                    this.lblLine3Stop6.Text = positionDesc;
                }
            }

            this.Refresh();
            this.Update();

            ////this.PlayMusic(lineCode, PositionType.Stop);
        }

        /// <summary>
        /// Get Unnormal Position Count
        /// </summary>
        /// <param name="positionStatus"></param>
        /// <returns></returns>
        public int GetUnnormalPositionCount(Dictionary<int, string> positionStatus, string lineCode)
        {
            int stopPositionCount = 0;
            int alarmPositionCount = 0;
            int helpPositionCount = 0;
            int maxUnnormalPositionCount = 0;

            foreach (int position in positionStatus.Keys)
            {
                if (!string.Equals("0000", positionStatus[position].ToString().Trim()))
                {
                    DataRow row = this.GetPositionDataRow(position, lineCode);

                    if (row != null)
                    {
                        PositionType positionType = this.GetPositionType(row);

                        if ((positionType == PositionType.Alarm)
                            || (positionType == PositionType.Unknown))
                        {
                            alarmPositionCount++;
                        }
                        else if (positionType == PositionType.Help)
                        {
                            helpPositionCount++;
                        }
                        else if (positionType == PositionType.Stop)
                        {
                            stopPositionCount++;
                        }
                    }
                }
            }

            maxUnnormalPositionCount = Math.Max(maxUnnormalPositionCount, stopPositionCount);
            maxUnnormalPositionCount = Math.Max(maxUnnormalPositionCount, alarmPositionCount);
            maxUnnormalPositionCount = Math.Max(maxUnnormalPositionCount, helpPositionCount);

            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                this.SetOperationArea(this.tlpOperationArea1, maxUnnormalPositionCount);
            }
            else if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                this.SetOperationArea(this.tlpOperationArea2, maxUnnormalPositionCount);
            }
            else if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                this.SetOperationArea(this.tlpOperationArea3, maxUnnormalPositionCount);
            }

            return maxUnnormalPositionCount;
        }

        /// <summary>
        /// Set Operation Area
        /// </summary>
        /// <param name="tlpOperationArea"></param>
        /// <param name="unnormalPositionCount"></param>
        public void SetOperationArea(TableLayoutPanel tlpOperationArea, int unnormalPositionCount)
        {
            if (unnormalPositionCount > 4)
            {
                tlpOperationArea.ColumnStyles.Clear();
                tlpOperationArea.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                tlpOperationArea.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33F));
                tlpOperationArea.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33F));
                tlpOperationArea.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33F));
            }
            else if (unnormalPositionCount > 2)
            {
                tlpOperationArea.ColumnStyles.Clear();
                tlpOperationArea.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                tlpOperationArea.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                tlpOperationArea.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                tlpOperationArea.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
            }
            else
            {
                tlpOperationArea.ColumnStyles.Clear();
                tlpOperationArea.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
                tlpOperationArea.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                tlpOperationArea.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
                tlpOperationArea.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 1F));
            }
        }

        /// <summary>
        /// Update Position Info
        /// </summary>
        /// <param name="positionStatus"></param>
        /// <param name="lineCode"></param>
        public void UpdatePositionInfo(Dictionary<int, string> positionStatus, string lineCode)
        {
            this.ClearPositionInfo(lineCode);
            int unnormalPositionCount = this.GetUnnormalPositionCount(positionStatus, lineCode);

            foreach (int position in positionStatus.Keys)
            {
                DataRow row = this.GetPositionDataRow(position, lineCode);
                PositionType positionType = this.GetPositionType(row);

                bool isShowPositionName = false;

                if (unnormalPositionCount <= 2)
                {
                    isShowPositionName = true;
                }
                else
                {
                    isShowPositionName = false;
                }

                string positionDesc = this.GetPositionDesc(row, isShowPositionName);
                string sPositionID = this.GetSPositionID(row);
                string fPositionID = this.GetFPositionID(row);
                PositionStatus pStatus = PositionStatus.Normal;

                if (!string.Equals("0000", positionStatus[position].ToString().Trim()))
                {
                    pStatus = PositionStatus.Unnormal;

                    if (!string.IsNullOrWhiteSpace(positionDesc))
                    {
                        if ((positionType == PositionType.Alarm)
                            || (positionType == PositionType.Unknown))
                        {
                            this.UpdateAlarmPositionInfo(positionDesc, lineCode);
                        }
                        else if (positionType == PositionType.Help)
                        {
                            this.UpdateHelpPositionInfo(positionDesc, lineCode);
                        }
                        else if (positionType == PositionType.Stop)
                        {
                            this.UpdateStopPositionInfo(positionDesc, lineCode);
                        }
                    }
                    else continue;
                }
                else
                {
                    pStatus = PositionStatus.Normal;
                }

                this.UpdatePositionLog(position, lineCode, positionType, sPositionID, fPositionID, pStatus);
            }

            try
            {
                this.StopMusic(lineCode);
            }
            catch 
            {
            }
        }

        private void StopMusic(string lineCode)
        {
            string musicName = string.Empty;

            AxWMPLib.AxWindowsMediaPlayer mediaPlayer = this.axWindowsMediaPlayer;

            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                if (this.lblLine1Alarm1.Text == string.Empty &&
                this.lblLine1Alarm2.Text == string.Empty &&
                this.lblLine1Alarm3.Text == string.Empty &&
                this.lblLine1Alarm4.Text == string.Empty &&
                this.lblLine1Alarm5.Text == string.Empty &&
                this.lblLine1Alarm6.Text == string.Empty)
                {
                    mediaPlayer = this.mediaPlayerAlarmLine1;
                    mediaPlayer.Ctlcontrols.stop();
                }
                if (
                this.lblLine1Help1.Text == string.Empty &&
                this.lblLine1Help2.Text == string.Empty &&
                this.lblLine1Help3.Text == string.Empty &&
                this.lblLine1Help4.Text == string.Empty &&
                this.lblLine1Help5.Text == string.Empty &&
                this.lblLine1Help6.Text == string.Empty)
                {
                    mediaPlayer = this.mediaPlayerHelpLine1;
                    mediaPlayer.Ctlcontrols.stop();
                }
                if (
                this.lblLine1Stop1.Text == string.Empty &&
                this.lblLine1Stop2.Text == string.Empty &&
                this.lblLine1Stop3.Text == string.Empty &&
                this.lblLine1Stop4.Text == string.Empty &&
                this.lblLine1Stop5.Text == string.Empty &&
                this.lblLine1Stop6.Text == string.Empty)
                {
                    mediaPlayer = this.mediaPlayerStopLine1;
                    mediaPlayer.Ctlcontrols.stop();
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                if (this.lblLine2Alarm1.Text == string.Empty &&
                this.lblLine2Alarm2.Text == string.Empty &&
                this.lblLine2Alarm3.Text == string.Empty &&
                this.lblLine2Alarm4.Text == string.Empty &&
                this.lblLine2Alarm5.Text == string.Empty &&
                this.lblLine2Alarm6.Text == string.Empty)
                {
                    mediaPlayer = this.mediaPlayerAlarmLine2;
                    mediaPlayer.Ctlcontrols.stop();
                }
                if (
                this.lblLine2Help1.Text == string.Empty &&
                this.lblLine2Help2.Text == string.Empty &&
                this.lblLine2Help3.Text == string.Empty &&
                this.lblLine2Help4.Text == string.Empty &&
                this.lblLine2Help5.Text == string.Empty &&
                this.lblLine2Help6.Text == string.Empty)
                {
                    mediaPlayer = this.mediaPlayerHelpLine2;
                    mediaPlayer.Ctlcontrols.stop();
                }
                if (
                this.lblLine2Stop1.Text == string.Empty &&
                this.lblLine2Stop2.Text == string.Empty &&
                this.lblLine2Stop3.Text == string.Empty &&
                this.lblLine2Stop4.Text == string.Empty &&
                this.lblLine2Stop5.Text == string.Empty &&
                this.lblLine2Stop6.Text == string.Empty)
                {
                    mediaPlayer = this.mediaPlayerStopLine2;
                    mediaPlayer.Ctlcontrols.stop();
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                if (this.lblLine3Alarm1.Text == string.Empty &&
                this.lblLine3Alarm2.Text == string.Empty &&
                this.lblLine3Alarm3.Text == string.Empty &&
                this.lblLine3Alarm4.Text == string.Empty &&
                this.lblLine3Alarm5.Text == string.Empty &&
                this.lblLine3Alarm6.Text == string.Empty)
                {
                    mediaPlayer = this.mediaPlayerAlarmLine3;
                    mediaPlayer.Ctlcontrols.stop();
                }
                if (
                this.lblLine3Help1.Text == string.Empty &&
                this.lblLine3Help2.Text == string.Empty &&
                this.lblLine3Help3.Text == string.Empty &&
                this.lblLine3Help4.Text == string.Empty &&
                this.lblLine3Help5.Text == string.Empty &&
                this.lblLine3Help6.Text == string.Empty)
                {
                    mediaPlayer = this.mediaPlayerHelpLine3;
                    mediaPlayer.Ctlcontrols.stop();
                }
                if (
                this.lblLine3Stop1.Text == string.Empty &&
                this.lblLine3Stop2.Text == string.Empty &&
                this.lblLine3Stop3.Text == string.Empty &&
                this.lblLine3Stop4.Text == string.Empty &&
                this.lblLine3Stop5.Text == string.Empty &&
                this.lblLine3Stop6.Text == string.Empty)
                {
                    mediaPlayer = this.mediaPlayerStopLine3;
                    mediaPlayer.Ctlcontrols.stop();
                }
            }
        }

        /// <summary>
        /// Update Position Log
        /// </summary>
        /// <param name="position"></param>
        /// <param name="lineCode"></param>
        /// <param name="positionType"></param>
        /// <param name="sPositionID"></param>
        /// <param name="fPositionID"></param>
        /// <param name="positionStatus"></param>
        private void UpdatePositionLog(int position, string lineCode, PositionType positionType, string sPositionID, string fPositionID, PositionStatus positionStatus)
        {
            string logDate = DateTime.Now.ToString("yyyy-MM-dd");
            string logTime = DateTime.Now.ToString("HH:mm:ss");
            string shift = string.Empty;
            string type = string.Empty;
            string stopTime = string.Empty;
            string helpTime = string.Empty;
            string alarmTime = string.Empty;
            TimeSpan span = new TimeSpan(0);
            bool isNeedUpdatePositionLog = false;

            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                if (positionStatus == PositionStatus.Unnormal)
                {
                    if (this.positionStatusInfoLine1.ContainsKey(position))
                    {
                        if (this.positionStatusInfoLine1[position].Status != positionStatus)
                        {
                            this.positionStatusInfoLine1[position].Status = positionStatus;
                            this.positionStatusInfoLine1[position].StatusBeginTime = DateTime.Now;
                        }
                    }
                    else
                    {
                        PositionStatusInfo positionStatusInfo = new PositionStatusInfo();
                        positionStatusInfo.Status = positionStatus;
                        positionStatusInfo.StatusBeginTime = DateTime.Now;

                        this.positionStatusInfoLine1.Add(position, positionStatusInfo);
                    }
                }
                else if (positionStatus == PositionStatus.Normal)
                {
                    if (this.positionStatusInfoLine1.ContainsKey(position))
                    {
                        if (this.positionStatusInfoLine1[position].Status != positionStatus)
                        {
                            this.positionStatusInfoLine1[position].Status = positionStatus;
                            this.positionStatusInfoLine1[position].StatusEndTime = DateTime.Now;

                            span = this.positionStatusInfoLine1[position].StatusEndTime
                                 - this.positionStatusInfoLine1[position].StatusBeginTime;

                            shift = this.GetShift(this.lblShiftLine1.Text);

                            isNeedUpdatePositionLog = true;
                        }
                    }
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                if (positionStatus == PositionStatus.Unnormal)
                {
                    if (this.positionStatusInfoLine2.ContainsKey(position))
                    {
                        if (this.positionStatusInfoLine2[position].Status != positionStatus)
                        {
                            this.positionStatusInfoLine2[position].Status = positionStatus;
                            this.positionStatusInfoLine2[position].StatusBeginTime = DateTime.Now;
                        }
                    }
                    else
                    {
                        PositionStatusInfo positionStatusInfo = new PositionStatusInfo();
                        positionStatusInfo.Status = positionStatus;
                        positionStatusInfo.StatusBeginTime = DateTime.Now;

                        this.positionStatusInfoLine2.Add(position, positionStatusInfo);
                    }
                }
                else if (positionStatus == PositionStatus.Normal)
                {
                    if (this.positionStatusInfoLine2.ContainsKey(position))
                    {
                        if (this.positionStatusInfoLine2[position].Status != positionStatus)
                        {
                            this.positionStatusInfoLine2[position].Status = positionStatus;
                            this.positionStatusInfoLine2[position].StatusEndTime = DateTime.Now;

                            span = this.positionStatusInfoLine2[position].StatusEndTime
                                 - this.positionStatusInfoLine2[position].StatusBeginTime;

                            shift = this.GetShift(this.lblShiftLine2.Text);

                            isNeedUpdatePositionLog = true;
                        }
                    }
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                if (positionStatus == PositionStatus.Unnormal)
                {
                    if (this.positionStatusInfoLine3.ContainsKey(position))
                    {
                        if (this.positionStatusInfoLine3[position].Status != positionStatus)
                        {
                            this.positionStatusInfoLine3[position].Status = positionStatus;
                            this.positionStatusInfoLine3[position].StatusBeginTime = DateTime.Now;
                        }
                    }
                    else
                    {
                        PositionStatusInfo positionStatusInfo = new PositionStatusInfo();
                        positionStatusInfo.Status = positionStatus;
                        positionStatusInfo.StatusBeginTime = DateTime.Now;

                        this.positionStatusInfoLine3.Add(position, positionStatusInfo);
                    }
                }
                else if (positionStatus == PositionStatus.Normal)
                {
                    if (this.positionStatusInfoLine3.ContainsKey(position))
                    {
                        if (this.positionStatusInfoLine3[position].Status != positionStatus)
                        {
                            this.positionStatusInfoLine3[position].Status = positionStatus;
                            this.positionStatusInfoLine3[position].StatusEndTime = DateTime.Now;

                            span = this.positionStatusInfoLine3[position].StatusEndTime
                                 - this.positionStatusInfoLine3[position].StatusBeginTime;

                            shift = this.GetShift(this.lblShiftLine3.Text);

                            isNeedUpdatePositionLog = true;
                        }
                    }
                }
            }

            if (isNeedUpdatePositionLog)
            {
                if ((positionType == PositionType.Alarm)
                    || (positionType == PositionType.Unknown))
                {
                    type = "A";
                    alarmTime = span.TotalSeconds.ToString("0");
                }
                else if (positionType == PositionType.Help)
                {
                    type = "H";
                    helpTime = span.TotalSeconds.ToString("0");
                }
                else if (positionType == PositionType.Stop)
                {
                    type = "S";
                    stopTime = span.TotalSeconds.ToString("0");
                }
                else
                {
                    return;
                }

                Position.UpdatePositionLogByKey(lineCode,
                                                logDate,
                                                shift,
                                                logTime,
                                                type,
                                                sPositionID,
                                                fPositionID,
                                                stopTime,
                                                helpTime,
                                                alarmTime);

                Position.UpdatePositionLogTempByKey(lineCode,
                                                    logDate,
                                                    shift,
                                                    logTime,
                                                    type,
                                                    sPositionID,
                                                    fPositionID,
                                                    stopTime,
                                                    helpTime,
                                                    alarmTime);
            }
        }

        /// <summary>
        /// Play Music
        /// </summary>
        /// <param name="lineCode"></param>
        /// <param name="positionType"></param>
        private void PlayMusic(string lineCode, PositionType positionType)
        {
            if (this.axWindowsMediaPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                return;
            }

            string musicName = string.Empty;

            ////AxWMPLib.AxWindowsMediaPlayer mediaPlayer = this.axWindowsMediaPlayer;

            if (string.Equals(ConfigInfo.LineCode1, lineCode))
            {
                if ((positionType == PositionType.Alarm)
                    || (positionType == PositionType.Unknown))
                {
                    musicName = ConfigInfo.GetAlarmMusicName(ConfigInfo.DtLineWatchSys1);

                    this.axWindowsMediaPlayer = this.mediaPlayerAlarmLine1;
                }
                else if (positionType == PositionType.Help)
                {
                    musicName = ConfigInfo.GetHelpMusicName(ConfigInfo.DtLineWatchSys1);

                    this.axWindowsMediaPlayer = this.mediaPlayerHelpLine1;
                }
                else if (positionType == PositionType.Stop)
                {
                    musicName = ConfigInfo.GetStopMusicName(ConfigInfo.DtLineWatchSys1);

                    this.axWindowsMediaPlayer = this.mediaPlayerStopLine1;
                }
            }

            if (string.Equals(ConfigInfo.LineCode2, lineCode))
            {
                if ((positionType == PositionType.Alarm)
                    || (positionType == PositionType.Unknown))
                {
                    musicName = ConfigInfo.GetAlarmMusicName(ConfigInfo.DtLineWatchSys2);

                    this.axWindowsMediaPlayer = this.mediaPlayerAlarmLine2;
                }
                else if (positionType == PositionType.Help)
                {
                    musicName = ConfigInfo.GetHelpMusicName(ConfigInfo.DtLineWatchSys2);

                    this.axWindowsMediaPlayer = this.mediaPlayerHelpLine2;
                }
                else if (positionType == PositionType.Stop)
                {
                    musicName = ConfigInfo.GetStopMusicName(ConfigInfo.DtLineWatchSys2);

                    this.axWindowsMediaPlayer = this.mediaPlayerStopLine2;
                }
            }

            if (string.Equals(ConfigInfo.LineCode3, lineCode))
            {
                if ((positionType == PositionType.Alarm)
                    || (positionType == PositionType.Unknown))
                {
                    musicName = ConfigInfo.GetAlarmMusicName(ConfigInfo.DtLineWatchSys3);

                    this.axWindowsMediaPlayer = this.mediaPlayerAlarmLine3;
                }
                else if (positionType == PositionType.Help)
                {
                    musicName = ConfigInfo.GetHelpMusicName(ConfigInfo.DtLineWatchSys3);

                    this.axWindowsMediaPlayer = this.mediaPlayerHelpLine3;
                }
                else if (positionType == PositionType.Stop)
                {
                    musicName = ConfigInfo.GetStopMusicName(ConfigInfo.DtLineWatchSys3);

                    this.axWindowsMediaPlayer = this.mediaPlayerStopLine3;
                }
            }

            if (!string.IsNullOrWhiteSpace(musicName))
            {
                string filePath = AppDomain.CurrentDomain.BaseDirectory
                                + ConfigurationManager.AppSettings["UploadFileDirectory"]
                                + "/" + lineCode + "/"
                                + musicName;

                FileInfo file = new FileInfo(filePath);
                if (file.Exists)
                {
                    try
                    {
                        this.axWindowsMediaPlayer.URL = file.FullName;
                        this.axWindowsMediaPlayer.Ctlcontrols.play();
                    }
                    catch { }
                }
            }
        }

        /// <summary>
        /// Has SHA Position
        /// </summary>
        /// <returns></returns>
        public bool HasSHAPosition()
        {
            if (this.hasSHAPositionLine1
                || this.hasSHAPositionLine2
                || this.hasSHAPositionLine3)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Change Shift Name
        /// </summary>
        /// <returns></returns>
        private bool ChangeShiftName()
        {
            bool isShiftNameChanged = false;

            string shiftInfoLine1 = string.Empty;
            string shiftInfoLine2 = string.Empty;
            string shiftInfoLine3 = string.Empty;

            if (ConfigInfo.CurrentLineCodeCount >= 1)
            {
                shiftInfoLine1 = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys1);

                if (!string.Equals(this.lblShiftLine1.Text.Trim(), shiftInfoLine1.Trim()))
                {
                    isShiftNameChanged = true;
                }
            }

            if (ConfigInfo.CurrentLineCodeCount >= 2)
            {
                shiftInfoLine2 = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys2);

                if (!string.Equals(this.lblShiftLine2.Text.Trim(), shiftInfoLine2.Trim()))
                {
                    isShiftNameChanged = true;
                }
            }

            if (ConfigInfo.CurrentLineCodeCount >= 3)
            {
                shiftInfoLine3 = ConfigInfo.GetShiftInfo(ConfigInfo.DtLineWatchSys3);

                if (!string.Equals(this.lblShiftLine3.Text.Trim(), shiftInfoLine3.Trim()))
                {
                    isShiftNameChanged = true;
                }
            }

            if (isShiftNameChanged)
            {
                this.lblShiftLine1.Text = shiftInfoLine1.Trim();
                this.lblShiftLine2.Text = shiftInfoLine2.Trim();
                this.lblShiftLine3.Text = shiftInfoLine3.Trim();
            }

            return isShiftNameChanged;
        }

        /// <summary>
        /// Get Shift
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private string GetShift(string text)
        {
            if (text.Contains("1"))
            {
                return "1";
            }
            else if (text.Contains("2"))
            {
                return "2";
            }

            return "0";
        }

        /// <summary>
        /// Play Music By Priority
        /// </summary>
        private void PlayMusicByPriority()
        {
            if ((ConfigInfo.CurrentLineCodeCount >= 1) &&
                (this.lblLine1Stop1.Text.Trim() != string.Empty ||
                 this.lblLine1Stop2.Text.Trim() != string.Empty ||
                 this.lblLine1Stop3.Text.Trim() != string.Empty ||
                 this.lblLine1Stop4.Text.Trim() != string.Empty ||
                 this.lblLine1Stop5.Text.Trim() != string.Empty ||
                 this.lblLine1Stop6.Text.Trim() != string.Empty))
            {
                this.PlayMusic(ConfigInfo.LineCode1, PositionType.Stop);
            }
            else if ((ConfigInfo.CurrentLineCodeCount >= 2) &&
             (this.lblLine2Stop1.Text.Trim() != string.Empty ||
              this.lblLine2Stop2.Text.Trim() != string.Empty ||
              this.lblLine2Stop3.Text.Trim() != string.Empty ||
              this.lblLine2Stop4.Text.Trim() != string.Empty ||
              this.lblLine2Stop5.Text.Trim() != string.Empty ||
              this.lblLine2Stop6.Text.Trim() != string.Empty))
            {
                this.PlayMusic(ConfigInfo.LineCode2, PositionType.Stop);
            }
            else if ((ConfigInfo.CurrentLineCodeCount >= 3) &&
            (this.lblLine3Stop1.Text.Trim() != string.Empty ||
             this.lblLine3Stop2.Text.Trim() != string.Empty ||
             this.lblLine3Stop3.Text.Trim() != string.Empty ||
             this.lblLine3Stop4.Text.Trim() != string.Empty ||
             this.lblLine3Stop5.Text.Trim() != string.Empty ||
             this.lblLine3Stop6.Text.Trim() != string.Empty))
            {
                this.PlayMusic(ConfigInfo.LineCode3, PositionType.Stop);
            }
            else if ((ConfigInfo.CurrentLineCodeCount >= 1) &&
                     (this.lblLine1Alarm1.Text.Trim() != string.Empty ||
                      this.lblLine1Alarm2.Text.Trim() != string.Empty ||
                      this.lblLine1Alarm3.Text.Trim() != string.Empty ||
                      this.lblLine1Alarm4.Text.Trim() != string.Empty ||
                      this.lblLine1Alarm5.Text.Trim() != string.Empty ||
                      this.lblLine1Alarm6.Text.Trim() != string.Empty))
            {
                this.PlayMusic(ConfigInfo.LineCode1, PositionType.Alarm);
            }
            else if ((ConfigInfo.CurrentLineCodeCount >= 2) &&
                 (this.lblLine2Alarm1.Text.Trim() != string.Empty ||
                  this.lblLine2Alarm2.Text.Trim() != string.Empty ||
                  this.lblLine2Alarm3.Text.Trim() != string.Empty ||
                  this.lblLine2Alarm4.Text.Trim() != string.Empty ||
                  this.lblLine2Alarm5.Text.Trim() != string.Empty ||
                  this.lblLine2Alarm6.Text.Trim() != string.Empty))
            {
                this.PlayMusic(ConfigInfo.LineCode2, PositionType.Alarm);
            }
            else if ((ConfigInfo.CurrentLineCodeCount >= 3) &&
                 (this.lblLine3Alarm1.Text.Trim() != string.Empty ||
                  this.lblLine3Alarm2.Text.Trim() != string.Empty ||
                  this.lblLine3Alarm3.Text.Trim() != string.Empty ||
                  this.lblLine3Alarm4.Text.Trim() != string.Empty ||
                  this.lblLine3Alarm5.Text.Trim() != string.Empty ||
                  this.lblLine3Alarm6.Text.Trim() != string.Empty))
            {
                this.PlayMusic(ConfigInfo.LineCode3, PositionType.Alarm);
            }
            else if ((ConfigInfo.CurrentLineCodeCount >= 1) &&
                     (this.lblLine1Help1.Text.Trim() != string.Empty ||
                      this.lblLine1Help2.Text.Trim() != string.Empty ||
                      this.lblLine1Help3.Text.Trim() != string.Empty ||
                      this.lblLine1Help4.Text.Trim() != string.Empty ||
                      this.lblLine1Help5.Text.Trim() != string.Empty ||
                      this.lblLine1Help6.Text.Trim() != string.Empty))
            {
                this.PlayMusic(ConfigInfo.LineCode1, PositionType.Help);
            }
            else if ((ConfigInfo.CurrentLineCodeCount >= 2) &&
                     (this.lblLine2Help1.Text.Trim() != string.Empty ||
                      this.lblLine2Help2.Text.Trim() != string.Empty ||
                      this.lblLine2Help3.Text.Trim() != string.Empty ||
                      this.lblLine2Help4.Text.Trim() != string.Empty ||
                      this.lblLine2Help5.Text.Trim() != string.Empty ||
                      this.lblLine2Help6.Text.Trim() != string.Empty))
            {
                this.PlayMusic(ConfigInfo.LineCode2, PositionType.Help);
            }
            else if ((ConfigInfo.CurrentLineCodeCount >= 3) &&
                     (this.lblLine3Help1.Text.Trim() != string.Empty ||
                      this.lblLine3Help2.Text.Trim() != string.Empty ||
                      this.lblLine3Help3.Text.Trim() != string.Empty ||
                      this.lblLine3Help4.Text.Trim() != string.Empty ||
                      this.lblLine3Help5.Text.Trim() != string.Empty ||
                      this.lblLine3Help6.Text.Trim() != string.Empty))
            {
                this.PlayMusic(ConfigInfo.LineCode3, PositionType.Help);
            }
        }

        /// <summary>
        /// Stop All Music
        /// </summary>
        private void StopAllMusic()
        {
            try
            {
                this.axWindowsMediaPlayer.Ctlcontrols.stop();
                this.mediaPlayerAlarmLine1.Ctlcontrols.stop();
                this.mediaPlayerHelpLine1.Ctlcontrols.stop();
                this.mediaPlayerStopLine1.Ctlcontrols.stop();
                this.mediaPlayerAlarmLine2.Ctlcontrols.stop();
                this.mediaPlayerHelpLine2.Ctlcontrols.stop();
                this.mediaPlayerStopLine2.Ctlcontrols.stop();
                this.mediaPlayerAlarmLine3.Ctlcontrols.stop();
                this.mediaPlayerHelpLine3.Ctlcontrols.stop();
                this.mediaPlayerStopLine3.Ctlcontrols.stop();
            }
            catch
            {
            }
        }

        /// <summary>
        /// Quit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.StopAllMusic();
            this.Hide();
        }

        /// <summary>
        /// timer Tick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Tick(object sender, EventArgs e)
        {
            this.ChangeShiftName();
        }

        /// <summary>
        /// timerMusic Tick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerMusic_Tick(object sender, EventArgs e)
        {
            this.PlayMusicByPriority();
        }
    }
}
